{% extends "base.html" %}

{% block breadcrumb_current %}
    公共参数  > 用户列表 > 新增用户
{% endblock %}
{% block content %}

    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                {{ message }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endfor %}
    {% endif %}
    
    <div class="bg-light p-3 shadow-sm">
        <h5 style="color: #417690;">
            新增用户
            <a href="{% url 'user_list' %}" class="btn btn-secondary btn-sm float-end" style="margin-left: 15px;">返回用户列表</a></h5>
    </div>


<div class="container" style="margin-top: 50px;">
    <div class="card">
    <div class="card-body">

    <form method="POST" action="{% url 'user_add' %}" novalidate>
        {% csrf_token %}
        
        <!-- 基础信息 -->
        <div class="form-section" style="margin-top: 20px;">
            <h4 class="mb-3">基础信息</h4>
            <div class="row">
                <!-- 用户名 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.username.id_for_label }}" class="form-label required">账号</label>
                    <input type="text" name="{{ form.username.name }}" id="{{ form.username.id_for_label }}" 
                           class="form-control {% if form.username.errors %}is-invalid{% endif %}" 
                           value="{{ form.username.value|default:'' }}" required>
                    {% if form.username.errors %}
                        <div class="invalid-feedback">{{ form.username.errors|join:", " }}</div>
                    {% endif %}
                </div>

                <!-- 姓名 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.realname.id_for_label }}" class="form-label required">姓名</label>
                    <input type="text" name="{{ form.realname.name }}" id="{{ form.realname.id_for_label }}" 
                           class="form-control {% if form.realname.errors %}is-invalid{% endif %}" 
                           value="{{ form.realname.value|default:'' }}" required>
                    {% if form.realname.errors %}
                        <div class="invalid-feedback">{{ form.realname.errors|join:", " }}</div>
                    {% endif %}
                </div>

                <!-- 手机号 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.phone_number.id_for_label }}" class="form-label required">手机号</label>
                    <input type="text" name="{{ form.phone_number.name }}" id="{{ form.phone_number.id_for_label }}" 
                           class="form-control {% if form.phone_number.errors %}is-invalid{% endif %}" 
                           value="{{ form.phone_number.value|default:'' }}" required>
                    {% if form.phone_number.errors %}
                        <div class="invalid-feedback">{{ form.phone_number.errors|join:", " }}</div>
                    {% endif %}
                </div>

                <!-- 密码 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.password1.id_for_label }}" class="form-label required">密码</label>
                    <input type="password" name="{{ form.password1.name }}" id="{{ form.password1.id_for_label }}" 
                           class="form-control {% if form.password1.errors %}is-invalid{% endif %}" required>
                    {% if form.password1.errors %}
                        <div class="invalid-feedback">{{ form.password1.errors|join:", " }}</div>
                    {% endif %}
                    <div class="form-text">{{ form.password1.help_text }}</div>
                </div>

                <!-- 确认密码 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.password2.id_for_label }}" class="form-label required">确认密码</label>
                    <input type="password" name="{{ form.password2.name }}" id="{{ form.password2.id_for_label }}" 
                           class="form-control {% if form.password2.errors %}is-invalid{% endif %}" required>
                    {% if form.password2.errors %}
                        <div class="invalid-feedback">{{ form.password2.errors|join:", " }}</div>
                    {% endif %}
                </div>

                <!-- 所属县区 -->
                <div class="col-md-2 mb-3">
                    <label for="{{ form.department.id_for_label }}" class="form-label required">所属县区</label>
                    <select name="{{ form.department.name }}" id="{{ form.department.id_for_label }}" 
                           class="form-control {% if form.department.errors %}is-invalid{% endif %}" required>
                        {% for value, text in form.department.field.choices %}
                            <option value="{{ value }}" {% if form.department.value == value %}selected{% endif %}>{{ text }}</option>
                        {% endfor %}
                    </select>
                    {% if form.department.errors %}
                        <div class="invalid-feedback">{{ form.department.errors|join:", " }}</div>
                    {% endif %}
                </div>

                <!-- 所属地市 -->
                {% if request.user.is_superuser %}
                <div class="col-md-2 mb-3">
                    <label for="{{ form.company.id_for_label }}" class="form-label required">所属地市</label>
                    {{ form.company }}
                    {% if form.company.errors %}
                        <div class="invalid-feedback">{{ form.company.errors|join:", " }}</div>
                    {% endif %}
                </div>
                {% else %}
                <input type="hidden" name="{{ form.company.name }}" value="{{ request.user.company.id }}">
                {% endif %}
            </div>
        </div>
        
        <!-- 权限设置 -->
        <div class="form-section" style="margin-top: 20px;">
            <h4 class="mb-3">权限设置</h4>
            <div class="row">
                <div class="col-md-2 mb-3">
                    <div class="form-check">
                        <input type="checkbox" name="{{ form.is_broad.name }}" id="{{ form.is_broad.id_for_label }}" 
                               class="form-check-input {% if form.is_broad.errors %}is-invalid{% endif %}"
                               {% if form.is_broad.value %}checked{% endif %}>
                        <label for="{{ form.is_broad.id_for_label }}" class="form-check-label">
                            管理所有县区
                        </label>
                    </div>
                </div>
                <div class="col-md-2 mb-3">
                    <div class="form-check">
                        <input type="checkbox" name="{{ form.is_approver.name }}" id="{{ form.is_approver.id_for_label }}" 
                               class="form-check-input {% if form.is_approver.errors %}is-invalid{% endif %}"
                               {% if form.is_approver.value %}checked{% endif %}>
                        <label for="{{ form.is_approver.id_for_label }}" class="form-check-label">
                            赋予审批权限
                        </label>
                    </div>
                </div>
                <div class="col-md-2 mb-3">
                    <div class="form-check">
                        <input type="checkbox" name="{{ form.is_deviceoperator.name }}" id="{{ form.is_deviceoperator.id_for_label }}" 
                               class="form-check-input {% if form.is_deviceoperator.errors %}is-invalid{% endif %}"
                               {% if form.is_deviceoperator.value %}checked{% endif %}>
                        <label for="{{ form.is_deviceoperator.id_for_label }}" class="form-check-label">
                            赋予设备操作权限
                        </label>
                    </div>
                </div>
                <div class="col-md-2 mb-3">
                    <div class="form-check">
                        <input type="checkbox" name="{{ form.is_inventoryoperator.name }}" id="{{ form.is_inventoryoperator.id_for_label }}" 
                               class="form-check-input {% if form.is_inventoryoperator.errors %}is-invalid{% endif %}"
                               {% if form.is_inventoryoperator.value %}checked{% endif %}>
                        <label for="{{ form.is_inventoryoperator.id_for_label }}" class="form-check-label">
                            赋予物料操作权限
                        </label>
                    </div>
                </div>
                <div class="col-md-2 mb-3">
                    <div class="form-check">
                        <input type="checkbox" name="{{ form.can_add_device.name }}" id="{{ form.can_add_device.id_for_label }}" 
                               class="form-check-input {% if form.can_add_device.errors %}is-invalid{% endif %}"
                               {% if form.can_add_device.value %}checked{% endif %}>
                        <label for="{{ form.can_add_device.id_for_label }}" class="form-check-label">
                            赋予增加设备权限
                        </label>
                    </div>
                </div>

            </div>
        </div>
        
        <!-- 操作按钮 -->
        <div class="row" style="margin-top: 40px;">
            <div class="col-md-12 text-center">
                <!-- <a href="{% url 'user_list' %}" class="btn btn-secondary">取 消</a> -->
                <button type="submit" class="btn ms-2" style="background-color: #417690; color: white;">保 存</button>            </div>
        </div>
    </form>
</div>
</div>
</div>
{% endblock content %}